package com.cskaoyan.controller.wxcontroller;

import com.cskaoyan.bean.LoginUser;
import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.pojo.UserExample;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.wxvo.UserIndexVO;
import com.cskaoyan.bean.wxvo.UserInfo;
import com.cskaoyan.bean.wxvo.UserLoginVO;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.MarketManagementService;
import com.cskaoyan.realm.MallToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author Gan
 * @Date 2021/7/11 16:06
 * @Description:
 */
@RestController
public class WxUserController {

    @Autowired
    MarketManagementService marketManagementService;

    @Autowired
    UserMapper userMapper;

    @PostMapping("wx/auth/login")
    public BaseRespVo WxLogin(@RequestBody LoginUser loginUser) {
        Subject subject = SecurityUtils.getSubject();
//        User user1 = (User) subject.getPrincipal();
        MallToken token = new MallToken(loginUser.getUsername(), loginUser.getPassword().toCharArray(), subject.getSession().getHost(), "wx");
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            return BaseRespVo.fail("账号或密码不正确！");
        }
        UserLoginVO userLoginVO = new UserLoginVO();
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(System.currentTimeMillis());
        userLoginVO.setTokenExpire(time);
        userLoginVO.setToken((String) subject.getSession().getId());
        //userInfo
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(user.getId());
        List<User> users = userMapper.selectByExample(userExample);
        User user1 = users.get(0);
        UserInfo userInfo = new UserInfo();
        userInfo.setNickName(user1.getNickname());
        userInfo.setAvatarUrl(user1.getAvatar());
        userLoginVO.setUserInfo(userInfo);
        return BaseRespVo.ok(userLoginVO);
    }

    @PostMapping("wx/auth/logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }


    @RequestMapping("wx/user/index")
    public BaseRespVo userIndex() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipals().getPrimaryPrincipal();
        UserIndexVO userIndexVO =
                marketManagementService.selectUserOrderStatus(user.getId());
        return BaseRespVo.ok(userIndexVO);

    }
}
